﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using TD.TheGame;

namespace TD.Interface.Menus
{
    internal class GameOverMenu : MenuBase
    {
        private Texture2D background;
        private Texture2D originalBackground;

        private Rectangle rectScore;
        private Rectangle rectWave;
        private Rectangle rectName;

        private Texture2D textureScore;
        private Texture2D textureWave;
        private Texture2D textureName;

        private string score;
        private string wave;
        private string name;
        private string previousName;

        public GameOverMenu(Game1 game) :
            base(game)
        {
            base.DrawOrder = 0x08C00000;
        }

        public override void Initialize()
        {
            rectScore = new Rectangle(1088, 393, 158, 40);
            rectWave = new Rectangle(1088, 451, 158, 40);
            rectName = new Rectangle(1088, 509, 158, 40);

            score = string.Empty;
            wave = string.Empty;
            name = string.Empty;
            previousName = name;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            ((Game1)Game).TextureManager.LoadAsset("interface\\game_defeat");
            ((Game1)Game).TextureManager.LoadAsset("interface\\defeat_scherm");
            background = ((Game1)Game).TextureManager.UseAsset("interface\\game_defeat");
            originalBackground = ((Game1)Game).TextureManager.UseAsset("interface\\defeat_scherm");

            textureScore = ((Game1)Game).TextureManager.UseAsset("default");
            textureWave = ((Game1)Game).TextureManager.UseAsset("default");
            textureName = ((Game1)Game).TextureManager.UseAsset("default");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            Dispose(ref background);
            Dispose(ref originalBackground);

            Dispose(ref textureScore);
            Dispose(ref textureWave);
            Dispose(ref textureName);

            base.UnloadContent();
        }
        private void Dispose(ref Texture2D texture)
        {
            if (texture != null)
            {
                texture.Dispose();
                texture = null;
            }
        }

        protected override void UpdateItems(List<DrawableGameComponent> items, GameTime gameTime)
        {
            // Do something...
        }

        protected override List<DrawableGameComponent> AddItems()
        {
            return new List<DrawableGameComponent>();
        }

        protected override void HandleEnabledChanged(object sender, EventArgs e)
        {
            // Do something...
        }

        protected override void HandleVisibleChanged(object sender, EventArgs e)
        {
            // Do something...
        }

        public override void Update(GameTime gameTime)
        {
            LevelComponent level = (LevelComponent)Game.Services.GetService(typeof(LevelComponent));

            if (((Game1)Game).InputManager.IsKeyPressed(Keys.Space))
            {
                Highscores.SetHighscore(level.Points, level.currLevel, name);
                //level.Reset();
                base.ExitMenu();
            }

            InputManager input = ((Game1)Game).InputManager;

            if (input.IsKeyPressed(Keys.A)) name += 'A';
            if (input.IsKeyPressed(Keys.B)) name += 'B';
            if (input.IsKeyPressed(Keys.C)) name += 'C';
            if (input.IsKeyPressed(Keys.D)) name += 'D';
            if (input.IsKeyPressed(Keys.E)) name += 'E';
            if (input.IsKeyPressed(Keys.F)) name += 'F';
            if (input.IsKeyPressed(Keys.G)) name += 'G';
            if (input.IsKeyPressed(Keys.H)) name += 'H';
            if (input.IsKeyPressed(Keys.I)) name += 'I';
            if (input.IsKeyPressed(Keys.J)) name += 'J';
            if (input.IsKeyPressed(Keys.K)) name += 'K';
            if (input.IsKeyPressed(Keys.L)) name += 'L';
            if (input.IsKeyPressed(Keys.M)) name += 'M';
            if (input.IsKeyPressed(Keys.N)) name += 'N';
            if (input.IsKeyPressed(Keys.O)) name += 'O';
            if (input.IsKeyPressed(Keys.P)) name += 'P';
            if (input.IsKeyPressed(Keys.Q)) name += 'Q';
            if (input.IsKeyPressed(Keys.R)) name += 'R';
            if (input.IsKeyPressed(Keys.S)) name += 'S';
            if (input.IsKeyPressed(Keys.T)) name += 'T';
            if (input.IsKeyPressed(Keys.U)) name += 'U';
            if (input.IsKeyPressed(Keys.V)) name += 'V';
            if (input.IsKeyPressed(Keys.W)) name += 'W';
            if (input.IsKeyPressed(Keys.X)) name += 'X';
            if (input.IsKeyPressed(Keys.Y)) name += 'Y';
            if (input.IsKeyPressed(Keys.Z)) name += 'Z';
            if (input.IsKeyPressed(Keys.Back))
            {
                if (name.Length > 0)
                {
                    name = name.Substring(0, name.Length - 1);
                }
            }

            if (name != previousName)
            {
                if (name.Length > 1)
                {
                    name = (name.Substring(0, 1).ToUpper() + name.Substring(1, name.Length - 1).ToLower());
                }
                else
                {
                    name.ToUpper();
                }

                if (font.MeasureString(name).X <= rectName.Width)
                {
                    previousName = name;
                    textureName = Masking.MaskString(Game, ref originalBackground, rectName, name);
                }
                else
                {
                    // Restore
                    name = previousName;
                }
            }

            if (level.Points.ToString() != score)
            {
                score = level.Points.ToString();
                textureScore = Masking.MaskString(Game, ref originalBackground, rectScore, score);
            }
            if (level.currLevel.ToString() != wave)
            {
                wave = level.currLevel.ToString();
                textureWave = Masking.MaskString(Game, ref originalBackground, rectWave, wave);
            }
            
            //base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the actual background.
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
            spriteBatch.Draw(background, new Rectangle(0, 0, 1280, 720), Color.White);
            spriteBatch.End();

            // Draw the score.
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(textureScore, new Vector2(rectScore.X, rectScore.Y), Color.White);
            spriteBatch.End();

            // Draw the wave.
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(textureWave, new Vector2(rectWave.X, rectWave.Y), Color.White);
            spriteBatch.End();

            // Draw the username.
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(textureName, new Vector2(rectName.X, rectName.Y), Color.White);
            spriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
